package cn.lixuan.service.impl;


import cn.lixuan.bo.CommentBO;
import cn.lixuan.constants.BaseInfoProperties;
import cn.lixuan.enums.YesOrNo;
import cn.lixuan.mapper.CommentMapper;
import cn.lixuan.mapper.CommentMapperCustom;
import cn.lixuan.pojo.Comment;
import cn.lixuan.service.CommentService;
import cn.lixuan.utils.PagedGridResult;
import cn.lixuan.utils.RedisOperator;
import cn.lixuan.vo.CommentVO;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CommentServiceImpl extends BaseInfoProperties implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentMapperCustom commentMapperCustom;

    @Autowired
    private Sid sid;

    @Autowired
    private RedisOperator redis;

    @Override
    public CommentVO createComment(CommentBO commentBO) {
        String commentId = sid.nextShort();

        Comment comment = new Comment();
        comment.setId(commentId);
        comment.setVlogerId(commentBO.getVlogerId());
        comment.setVlogId(commentBO.getVlogId());

        comment.setCommentUserId(commentBO.getCommentUserId());
        comment.setFatherCommentId(commentBO.getFatherCommentId());

        comment.setContent(commentBO.getContent());
        comment.setLikeCounts(0);
        comment.setCreateTime(new Date());

        commentMapper.insert(comment);
        redis.increment(BaseInfoProperties.REDIS_VLOG_COMMENT_COUNTS +commentBO.getVlogId(), 1);

        // 留言后最新评论需要返回前端进行展示
        CommentVO commentVO = new CommentVO();
        BeanUtils.copyProperties(comment, commentVO);

        return commentVO;
    }

    @Override
    public PagedGridResult queryVlogComments(String vlogId, String userId, Integer page, Integer pageSize) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("vlogId", vlogId);

        PageHelper.startPage(page, pageSize);

        List<CommentVO> list = commentMapperCustom.getCommentList(map);

        for (CommentVO cv : list) {
            String commentId = cv.getCommentId();

            // 当前短视频某个评论的点赞数
            String countsStr = redis.getHashValue(BaseInfoProperties.REDIS_VLOG_COMMENT_LIKED_COUNTS, commentId);
            Integer counts = 0;

            if(StringUtils.isNotBlank(countsStr)) {
                counts = Integer.valueOf(countsStr);
            }
            cv.setLikeCounts(counts);

            // 判断当前用户是否点赞过该评论
            String doILike = redis.hget(BaseInfoProperties.REDIS_USER_LIKE_COMMENT, userId + ":" + commentId);

            if(StringUtils.isNotBlank(doILike) && doILike.equalsIgnoreCase("1")) {
                    cv.setIsLike(YesOrNo.YES.type);
            }
        }

        return setterPagedGrid(list, page);
    }

    @Override
    public void deleteComment(String commentUserId, String commentId, String vlogId) {

        Comment pendingDelete = new Comment();
        pendingDelete.setId(commentId);
        pendingDelete.setCommentUserId(commentUserId);

        commentMapper.delete(pendingDelete);

        redis.decrement(BaseInfoProperties.REDIS_VLOG_COMMENT_COUNTS +vlogId, 1);
    }
}
